<?php
namespace app\agent\controller;

use app\common\controller\AgentBaseController;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class InsuranceTotal extends AgentBaseController{

    /**
     * @功能：根据用户统计收藏
     * @开发者：cxl
     * @return string
     */
    public function user_favorite_total(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $nick = IRequest::get("nick");
        $phone = IRequest::get("phone");

        //判断是否为用户搜索
        $insurance_favorite_where['lo.is_delete'] = IsDelete::No;
        if(!empty($user_id)) $insurance_favorite_where['lo.user_id'] = $user_id;
        if(!empty($nick)) $insurance_favorite_where['use.nick'] = array("like", "%{$nick}%");;
        if(!empty($phone)) $insurance_favorite_where['use.phone'] = array("like", "%{$phone}%");

        //取得数据
        $insurance_favorite_list = IDb::getInstance("insurance_favorite as lo")
            ->setDbFiled("lo.user_id,count(*) as favorite_num")
            ->setDbJoin("user as use","use.user_id=lo.user_id")
            ->setDbWhere($insurance_favorite_where)
            ->setDbGroup("lo.user_id")
            ->setDbOrder("favorite_num desc,lo.user_id")
            ->pag();

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("insurance_favorite_list",$insurance_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：保险产品收藏记录
     * @开发者：cxl
     * @return string
     */
    public function user_favorite_list(){
        //接收参数
        $user_id = IRequest::get("user_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //取得数据
        $insurance_favorite_where['is_delete'] = IsDelete::No;
        $insurance_favorite_where['user_id'] = $user_id;
        $insurance_favorite_list = IDb::getInstance("insurance_favorite")
            ->setDbWhere($insurance_favorite_where)
            ->setDbOrder("insurance_favorite_id")
            ->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("insurance_favorite_list",$insurance_favorite_list);

        //渲染模板输出
        return view();
    }

//    /**
//     * @功能：根据保险产品统计收藏
//     * @开发者：cxl
//     * @return string
//     */
//    public function insurance_favorite_total(){
//        //接收参数
//        $insurance_product_id = IRequest::get("insurance_product_id");
//        $insurance_product_name = IRequest::get("insurance_product_name");
//
//        //判断是否为用户搜索
//        $insurance_favorite_where['is_delete'] = IsDelete::No;
//        if(!empty($insurance_product_id)){
//            $insurance_favorite_where['insurance_product_id'] = $insurance_product_id;
//        }else {
//            if (empty($insurance_product_id) && !empty($insurance_product_name)) {
//                //查询条件
//                $insurance_product_where['insurance_product_name'] = array("like", "%{$insurance_product_name}%");
//
//                //取得用户信息
//                $insurance_product_list = IDb::getInstance("insurance_product")->setDbWhere($insurance_product_where)->sel();
//                if (!empty($insurance_product_list) && is_array($insurance_product_list)) {
//                    $insurance_favorite_where['insurance_product_id'] = array("in", array_column($insurance_product_list, "insurance_product_id"));
//                }
//            }
//        }
//
//        //取得数据
//        $insurance_favorite_list = IDb::getInstance("insurance_favorite")
//            ->setDbFiled("insurance_product_id,count(*) as favorite_num")
//            ->setDbWhere($insurance_favorite_where)
//            ->setDbGroup("insurance_product_id")
//            ->setDbOrder("favorite_num desc,insurance_product_id")
//            ->pag();
//
//        //页面赋值
//        $this->assign("insurance_product_id",$insurance_product_id);
//        $this->assign("insurance_product_name",$insurance_product_name);
//        $this->assign("insurance_favorite_list",$insurance_favorite_list);
//
//        //渲染模板输出
//        return view();
//    }
    /**
     * @功能：根据保险产品统计收藏
     * @开发者：cxl
     * @return string
     */
    public function insurance_favorite_total(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id");
        $insurance_product_name = IRequest::get("insurance_product_name");

        //判断是否为用户搜索
        $insurance_favorite_where['cf.is_delete'] = IsDelete::No;
        $insurance_favorite_where['cp.agent_id'] = get_login_agent_id();
        if (!empty($insurance_product_id)) $insurance_favorite_where['cf.insurance_product_id'] = $insurance_product_id;
        if (!empty($insurance_product_name)) $insurance_favorite_where['cp.insurance_product_name'] = array("like", "%{$insurance_product_name}%");

        //取得数据
        $insurance_favorite_list = IDb::getInstance("insurance_favorite as cf")
            ->setDbFiled("cf.insurance_product_id,count(*) as favorite_num")
            ->setDbJoin('insurance_product as cp','cp.insurance_product_id = cf.insurance_product_id')
            ->setDbWhere($insurance_favorite_where)
            ->setDbGroup("cf.insurance_product_id")
            ->setDbOrder("favorite_num desc,cf.insurance_product_id")
            ->pag();
        if ($insurance_favorite_list === false){
            error('获取收藏统计失败');
        }

        //页面赋值
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_product_name",$insurance_product_name);
        $this->assign("insurance_favorite_list",$insurance_favorite_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：根据保险产品收藏记录
     * @开发者：cxl
     * @return string
     */
    public function insurance_favorite_list(){
        //接收参数
        $insurance_product_id = IRequest::get("insurance_product_id",IRequest::NOT_EMPTY,"用户编号不能为空！");

        //取得数据
        $insurance_favorite_where['is_delete'] = IsDelete::No;
        $insurance_favorite_where['insurance_product_id'] = $insurance_product_id;
        $insurance_favorite_list = IDb::getInstance("insurance_favorite")
            ->setDbWhere($insurance_favorite_where)
            ->setDbOrder("insurance_favorite_id")
            ->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("insurance_product_id",$insurance_product_id);
        $this->assign("insurance_favorite_list",$insurance_favorite_list);

        //渲染模板输出
        return view();
    }
}
